<?
class Info extends Beacon {
	function Info (){
	}
	function Generate($Process) {
		switch($this->Beacon) {
			case "INFO":
				$Process=Beacon::Generate($Process);
				$Vars = $Process->processingVars($this->Vars);
				$Vars = explode("|",$Vars);
				$this->Attributes = $Vars;
				$Process = $this->getInfos($Process);
				break;
			case "COUNT":
				$Vars = explode("|",$this->Vars);
				$this->Attributes = $Vars;
				$Process = $this->getCount($Process);
				break;
		}
		return $Process;
	}
	function getInfos($Process){
		//Le premier parametre est la requete
		$Query = $this->Attributes[0];
		if (!$Query) return $Process;
		//Le second est le nom de la variable dans laquelle stoquer linfo (INFO par defaut)
		$Var = (!empty($this->Attributes[1]))?$this->Attributes[1]:"INFO";
		//On extrait le nom du module
		$Mod = explode("/",$Query,2);
		$Module = $Mod[0];
		if (!$Module) return $Process;
		if ($GLOBALS["Systeme"]->isModule($Module)){
			$Query = $Mod[1];
			//On recolte les infos sur la requete
			$Result = $GLOBALS["Systeme"]->Modules[$Module]->splitQuery($Query);
			unset($Process->TempVar[$Var]);
			//On recupere le type de la reponse
			$Type = $Result[sizeof($Result)-1]["DataSource"];
			//On prends les infos sur l objectclass
			if ($Type){
				$GLOBALS['Systeme']->Modules[$Module]->loadSchema();
				$R = $GLOBALS['Systeme']->Modules[$Module]->getByTitleOrFkey($Type);
				if ($R["Type"]==2) $Type = $R["Nom"];
				$Special = $GLOBALS['Systeme']->Modules[$Module]->isReflexive($Type);
                                $IdObjClass = $GLOBALS['Systeme']->Modules[$Module]->Db->findByTitle($Type);
				$Out["Reflexive"] = $Special;
				$Out["Historique"] = $Result;
				$Out["Module"] = $Module;
				$Out["NbHisto"] = ($Result[0]["Value"])?sizeof($Result):0;
				$Out["TypeSearch"] = $Result[0]["Type"];
				$Out["TypeChild"] = ($Result[0]["Type"]=="Parent")?$Result[0]["DataSource"]:$Result[sizeof($Result)-1]["DataSource"];
				if ($Out["TypeSearch"]=="Child")$Out["ObjectType"]= $Result[sizeof($Result)-2]["DataSource"];
				else $Out["ObjectType"] =$Result[sizeof($Result)-1]["DataSource"];
				$Enfants = $GLOBALS['Systeme']->Modules[$Module]->Db->ObjectClass[$IdObjClass]->parentOf;
				$Out["typesEnfant"] = $Enfants;
				$Parents = $GLOBALS['Systeme']->Modules[$Module]->Db->ObjectClass[$IdObjClass]->childOf;
				$Out["typesParent"] = $Parents;
				//Fonctions
				$Functions = $GLOBALS['Systeme']->Modules[$Module]->Db->ObjectClass[$IdObjClass]->Functions;
				$Out["Functions"] = $Functions;
				$Temp = $Module;
				$x = (is_array($Result[0]["Interface"]))?0:1;
				if (!$x)$Out["TypeSearch"] = "Interface";
				for ($i=0;$i<sizeof($Result)-$x;$i++) {
					$Temp.="/".$Result[$i]["DataSource"]."/".$Result[$i]["Value"];
				}
				$Out["LastDirect"] = $Temp;
				$Process->TempVar[$Var]=$Out;
			}else{
				//Cas dune url menu
				$R = explode("/",$Query);
				if (is_array($R))foreach ($R as $r){
					$T["Value"] = $r;
					$Result[] = $T;
				}
				$Out["Historique"] = $Result;
				$Out["NbHisto"] = sizeof($Result);
				$Process->TempVar[$Var]=$Out;
			}
		}else{
			//Cas dune url menu
			$R = explode("/",$Query);
			if (is_array($R))foreach ($R as $r){
				$T["Value"] = $r;
				$Result[] = $T;
			}
			$Out["Historique"] = $Result;
			$Out["NbHisto"] = sizeof($Result);
			$Process->TempVar[$Var]=$Out;
		}
		return $Process;
	}
    function getCount($Process){
        //Le premier parametre est la requete
        $Query = explode("[!",$this->Attributes[0]);
        if (sizeof($Query)>1){
            $Query = $Process->processingVars($this->Attributes[0]);
        }else{
            $Query=$Query[0];
        }
        //Le second est le nom de la variable dans laquelle stoquer l'info (COUNT par defaut)
        $Var = (!empty($this->Attributes[1]))?$this->Attributes[1]:"COUNT";
        if (is_string($Query)){
            $V = ($this->Attributes[2]!='')?"m.".$this->Attributes[2]:"m.Id";
            //On extrait le nom du module
            $Module = explode("/",$Query,2);
            $Query = $Module[1];
            $Module = $Module[0];
            //On recolte les infos sur la requete
            //Execution de la requete
            if ($Module)if (is_object($GLOBALS['Systeme']->Modules[$Module]))$Tab=$GLOBALS['Systeme']->Modules[$Module]->callData($Query,"",0,1000000,"","","COUNT(DISTINCT(".$V."))");
            unset($Process->TempVar[$Var]);
            $Count=0;
            if (sizeof($Tab)>1) foreach ($Tab as $T) $Count+=$T["COUNT(DISTINCT(".$V."))"];
            else $Count=$Tab[0]["COUNT(DISTINCT(".$V."))"];
            $Process->TempVar[$Var]=$Count;
        }elseif(is_array($Query)) {
            unset($Process->TempVar[$Var]);
            $Process->TempVar[$Var]=sizeof($Query);
        }
        return $Process;
    }
    function Affich() {
		//return "INFO BEACON";
	}
}
?>
